# -*- coding: utf-8 -*-
import sys

from client.pages.LoginPage import *
from client.pages.RegisterPage import *
from client.pages.UserPage import *

import ctypes

# 告诉 Windows 这个窗口强制使用单独的AppUserModelID ，现在这个窗口拥有了一个新资源支配权限，方便改任务栏图标.
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("PyChat")


def ensure_dir():
    dirs = ["client/.cache/", "client/.cache/img_buf/", "client/history/"]
    for path in dirs:
        if not os.path.exists(path):
            os.mkdir(path)


def login2register():
    register_page.show()
    login_page.hide()


def register2login():
    login_page.show()
    register_page.hide()


def login2user(uid):
    login_page.hide()
    user_page.uid = uid
    user_page.update()
    user_page.show()
    user_page.update_thread.uid = uid
    user_page.update_thread.start()


def user2login():
    user_page.hide()
    login_page.info_label.setText("")
    login_page.show()


if __name__ == "__main__":
    ensure_dir()
    app = QApplication(sys.argv)
    login_page = LoginPage()
    login_page.show()
    register_page = RegisterPage()
    user_page = UserMainPage()

    login_page.register_signal.connect(login2register)
    register_page.login_signal.connect(register2login)
    login_page.login_signal.connect(login2user)
    user_page.switch_login_signal.connect(user2login)
    sys.exit(app.exec_())
